
# 运行应用
function RunApplication($curPath,$name,$param) {    
    Start-Job -ScriptBlock{
        #进入项目
        $currentApplicationPath = $curPath+'\'+$name+'\'
        cd $currentApplicationPath
        #编译打包
        mvn clean package
        #运行 jar
        $targetPath = '.\'+$name+'\target\'+$name+'-0.0.1-SNAPSHOT.jar'
        & java -jar $targetPath+' '+$param
    }    
}

# 检查服务是否启动
function CheckWebIsStart($url) {
    Start-Job -ScriptBlock{
       # 检查Eureka已经启动
        for ($i = 0; $i -lt 120; $i++) {
            $x= curl -URi $url | Select -ExpandProperty StatusCode;
            if ($x -eq 200) {
                break;
            }
            sleep 1;
        }

        if($x -ne 200){
            Read-Host "Waitting for euraka faile,press any key to exit";
            exit;
        }   
    }
}

# 获取当前路径
$curPath =  (gi .).FullName

# 运行Eureka peer1
RunApplication $curPath 'euraka' '--spring-profiles-active=peer1'
# 运行Eureka peer2
RunApplication $curPath 'euraka' '--spring-profiles-active=peer2'

# 检查Eureka启动完成
CheckWebIsStart("http://127.0.0.1:1111")

# 启动config1
RunApplication $curPath 'euraka' '--server.port=7001'
# 启动config2
RunApplication $curPath 'euraka' '--server.port=7002'

# 检查config启动完成
CheckWebIsStart("http://127.0.0.1:7001")

# 普通服务1

# 普通服务2

# 启动网关

Read-Host "Finish start all website,press any key to exit";
exit